﻿using System.Collections.Generic;
using System;
using System.Text;
using Evaluant.NLinq.Expressions;
using ANLP;
namespace Evaluant.NLinq
{
    public enum Tokens : int
    {
        Whitespace,
        BracketOpen,
        BracketClose,
        BraceOpen,
        BraceClose,
        ParenthesisOpen,
        ParenthesisClose,
        QuestionMark,
        Colon,
        BooleanOr,
        BooleanAnd,
        Not,
        COMMA,
        Plus,
        Minus,
        Multiply,
        Divide,
        Modulo,
        Equals,
        NotEquals,
        LessThan,
        LessThanOrEquals,
        GreaterThan,
        GreaterThanOrEquals,
        Dot,
        New,
        Equal,
        Arrow,
        INTEGER,
        FLOAT,
        STRING,
        ID,
        TRUE,
        FALSE,
        DATETIME,
        FROM,
        IN,
        INTO,
        WHERE,
        LET,
        JOIN,
        ON,
        EQUALS,
        ORDERBY,
        SELECT,
        GROUP,
        BY,
        ASCENDING,
        DESCENDING,
    }

    [System.Diagnostics.DebuggerNonUserCode]
    public partial class Parser : ANLP.Parser<Expression>
    {
        public Parser(Lexer lexer) : base(lexer) { }
        static Parser()
        {
            states = new int[][]{
		new int[]{ -0, },
		new int[]{ -1, },
		new int[]{ -3, -2, },
		new int[]{ (int)Tokens.BracketOpen, -1, (int)Tokens.BracketClose, },
		new int[]{ },
		new int[]{ -5, -4, },
		new int[]{ (int)Tokens.QuestionMark, -5, (int)Tokens.Colon, -5, },
		new int[]{ },
		new int[]{ -8, -6, },
		new int[]{ -7, },
		new int[]{ },
		new int[]{ (int)Tokens.BooleanOr, -8, },
		new int[]{ -11, -9, },
		new int[]{ -10, },
		new int[]{ },
		new int[]{ (int)Tokens.BooleanAnd, -11, },
		new int[]{ -14, -12, },
		new int[]{ -13, },
		new int[]{ },
		new int[]{ (int)Tokens.Equals, -14, },
		new int[]{ (int)Tokens.NotEquals, -14, },
		new int[]{ -17, -15, },
		new int[]{ -16, },
		new int[]{ },
		new int[]{ (int)Tokens.LessThan, -17, },
		new int[]{ (int)Tokens.LessThanOrEquals, -17, },
		new int[]{ (int)Tokens.GreaterThan, -17, },
		new int[]{ (int)Tokens.GreaterThanOrEquals, -17, },
		new int[]{ -20, -18, },
		new int[]{ -19, },
		new int[]{ },
		new int[]{ (int)Tokens.Plus, -20, },
		new int[]{ (int)Tokens.Minus, -20, },
		new int[]{ -23, -21, },
		new int[]{ -22, },
		new int[]{ },
		new int[]{ (int)Tokens.Multiply, -23, },
		new int[]{ (int)Tokens.Divide, -23, },
		new int[]{ (int)Tokens.Modulo, -23, },
		new int[]{ (int)Tokens.Not, -24, },
		new int[]{ (int)Tokens.Minus, -24, },
		new int[]{ -24, },
		new int[]{ -27, -25, },
		new int[]{ -26, },
		new int[]{ },
		new int[]{ (int)Tokens.Dot, -27, },
		new int[]{ -28, },
		new int[]{ (int)Tokens.ParenthesisOpen, -1, (int)Tokens.ParenthesisClose, },
		new int[]{ -34, },
		new int[]{ -29, },
		new int[]{ -32, },
		new int[]{ -38, },
		new int[]{ (int)Tokens.INTEGER, },
		new int[]{ (int)Tokens.FLOAT, },
		new int[]{ (int)Tokens.STRING, },
		new int[]{ -62, },
		new int[]{ -30, },
		new int[]{ -32, -60, },
		new int[]{ (int)Tokens.ParenthesisOpen, -33, (int)Tokens.ParenthesisClose, (int)Tokens.Arrow, -31, },
		new int[]{ -32, (int)Tokens.Arrow, -31, },
		new int[]{ (int)Tokens.BraceOpen, -1, (int)Tokens.BraceClose, },
		new int[]{ -1, },
		new int[]{ (int)Tokens.ID, },
		new int[]{ (int)Tokens.COMMA, -32, },
		new int[]{ -32, },
		new int[]{ (int)Tokens.New, -37, (int)Tokens.BraceOpen, -35, (int)Tokens.BraceClose, },
		new int[]{ (int)Tokens.New, -57, -60, },
		new int[]{ (int)Tokens.COMMA, -36, },
		new int[]{ -36, },
		new int[]{ -32, (int)Tokens.Equal, -1, },
		new int[]{ -1, },
		new int[]{ },
		new int[]{ -39, -40, },
		new int[]{ (int)Tokens.FROM, -57, -32, (int)Tokens.IN, -1, },
		new int[]{ (int)Tokens.FROM, -32, (int)Tokens.IN, -1, },
		new int[]{ -41, -43, -44, },
		new int[]{ -43, -44, },
		new int[]{ -42, },
		new int[]{ -39, },
		new int[]{ -47, },
		new int[]{ -46, },
		new int[]{ -48, },
		new int[]{ -52, },
		new int[]{ -55, },
		new int[]{ -56, },
		new int[]{ -45, },
		new int[]{ },
		new int[]{ (int)Tokens.INTO, -32, -40, },
		new int[]{ (int)Tokens.WHERE, -1, },
		new int[]{ (int)Tokens.LET, -32, (int)Tokens.Equal, -1, },
		new int[]{ (int)Tokens.JOIN, -57, -32, (int)Tokens.IN, -1, (int)Tokens.ON, -1, (int)Tokens.EQUALS, -1, -49, },
		new int[]{ (int)Tokens.JOIN, -32, (int)Tokens.IN, -1, (int)Tokens.ON, -1, (int)Tokens.EQUALS, -1, -49, },
		new int[]{ -50, },
		new int[]{ },
		new int[]{ (int)Tokens.INTO, -32, },
		new int[]{ (int)Tokens.ORDERBY, -1, -53, },
		new int[]{ (int)Tokens.COMMA, -51, },
		new int[]{ -51, },
		new int[]{ -54, },
		new int[]{ },
		new int[]{ (int)Tokens.ASCENDING, },
		new int[]{ (int)Tokens.DESCENDING, },
		new int[]{ (int)Tokens.SELECT, -1, },
		new int[]{ (int)Tokens.GROUP, -32, (int)Tokens.BY, -1, },
		new int[]{ -58, },
		new int[]{ -32, -58, },
		new int[]{ -32, },
		new int[]{ (int)Tokens.Dot, -32, },
		new int[]{ (int)Tokens.COMMA, -1, },
		new int[]{ -1, },
		new int[]{ (int)Tokens.ParenthesisOpen, -61, (int)Tokens.ParenthesisClose, },
		new int[]{ -59, },
		new int[]{ },
		new int[]{ (int)Tokens.TRUE, },
		new int[]{ (int)Tokens.FALSE, },
};
            rules = new int[][]{
new int[]{ -1, },
new int[]{ -2, },
new int[]{ -3, -4, },
new int[]{ -5, },
new int[]{ -6, -7, },
new int[]{ -8, },
new int[]{ -9, -10, },
new int[]{ -11, },
new int[]{ -12, },
new int[]{ -13, -14, },
new int[]{ -15, },
new int[]{ -16, },
new int[]{ -17, -18, },
new int[]{ -19, -20, },
new int[]{ -21, },
new int[]{ -22, -23, },
new int[]{ -24, -25, -26, -27, },
new int[]{ -28, },
new int[]{ -29, -30, },
new int[]{ -31, -32, },
new int[]{ -33, },
new int[]{ -34, -35, },
new int[]{ -36, -37, -38, },
new int[]{ -39, -40, -41, },
new int[]{ -42, },
new int[]{ -43, -44, },
new int[]{ 26, -45, },
new int[]{ -46, -47, -48, -49, -50, -51, },
new int[]{ -52, -53, -54, -55, -56, },
new int[]{ -57, },
new int[]{ -58, -59, },
new int[]{ -60, -61, },
new int[]{ -62, },
new int[]{ 33, -63, -64, },
new int[]{ -65, -66, },
new int[]{ 35, -67, -68, },
new int[]{ -69, -70, },
new int[]{ 57, -71, },
new int[]{ -72, },
new int[]{ -73, -74, },
new int[]{ -75, -76, },
new int[]{ 41, -77, },
new int[]{ -78, -79, -80, -81, -82, },
new int[]{ -83, -84, },
new int[]{ -85, -86, },
new int[]{ -87, },
new int[]{ -88, },
new int[]{ -89, },
new int[]{ -90, -91, },
new int[]{ -92, -93, },
new int[]{ -94, },
new int[]{ -95, },
new int[]{ 52, -96, -97, },
new int[]{ -98, -99, },
new int[]{ -100, -101, },
new int[]{ -102, },
new int[]{ -103, },
new int[]{ -104, -105, -106, },
new int[]{ 58, -107, },
new int[]{ 59, -108, -109, },
new int[]{ -110, },
new int[]{ -111, -112, },
new int[]{ -113, -114, },
};
        }
        static int[][] states, rules;
        protected override int[][] States
        {
            get { return states; }
        }

        protected override int[][] Rules
        {
            get { return rules; }
        }

        protected override void DoAction(int state, int step, ParserExpression<Expression>[] expressions)
        {
            switch (state)
            {
                case 0:
                    if (step == int.MaxValue)
                        expressions[expressions.Length - 1] = expressions[0];
                    break;
                case 1:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = expressions[0].Value; }
                                    break;
                                }
                        }
                        break;
                    }
                case 2:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = expressions[0].Value; }
                                    break;
                                }
                            case 1:
                                {
                                    { if (expressions[1].Value != null) { ((Indexer)expressions[1].Value).InnerExpression = expressions[expressions.Length - 1].Value; expressions[expressions.Length - 1].Value = expressions[1].Value; } }
                                    break;
                                }
                        }
                        break;
                    }
                case 3:
                    {
                        switch (step)
                        {
                            case 2:
                                {
                                    { expressions[expressions.Length - 1].Value = new Indexer(null, expressions[2].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 5:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = expressions[0].Value; }
                                    break;
                                }
                        }
                        break;
                    }
                case 6:
                    {
                        switch (step)
                        {
                            case 3:
                                {
                                    { expressions[expressions.Length - 1].Value = new TernaryExpression(expressions[0].Value, expressions[2].Value, expressions[4].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 8:
                    {
                        switch (step)
                        {
                            case 1:
                                {
                                    { if (expressions[1].Value != null) { expressions[expressions.Length - 1].Value = expressions[1].Value; ((BinaryExpression)expressions[expressions.Length - 1].Value).LeftExpression = expressions[0].Value; } else { expressions[expressions.Length - 1].Value = expressions[0].Value; } }
                                    break;
                                }
                        }
                        break;
                    }
                case 9:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = expressions[0].Value; }
                                    break;
                                }
                        }
                        break;
                    }
                case 11:
                    {
                        switch (step)
                        {
                            case 1:
                                {
                                    { expressions[expressions.Length - 1].Value = new BinaryExpression(BinaryExpressionType.Or, null, expressions[1].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 12:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = expressions[0].Value; }
                                    break;
                                }
                            case 1:
                                {
                                    { if (expressions[1].Value != null) { expressions[expressions.Length - 1].Value = expressions[1].Value; ((BinaryExpression)expressions[expressions.Length - 1].Value).LeftExpression = expressions[0].Value; } }
                                    break;
                                }
                        }
                        break;
                    }
                case 13:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = expressions[0].Value; }
                                    break;
                                }
                        }
                        break;
                    }
                case 15:
                    {
                        switch (step)
                        {
                            case 1:
                                {
                                    { expressions[expressions.Length - 1].Value = new BinaryExpression(BinaryExpressionType.And, null, expressions[1].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 16:
                    {
                        switch (step)
                        {
                            case 1:
                                {
                                    { if (expressions[1].Value == null) { expressions[expressions.Length - 1].Value = expressions[0].Value; } else { ((BinaryExpression)expressions[1].Value).LeftExpression = expressions[0].Value; expressions[expressions.Length - 1].Value = expressions[1].Value; } }
                                    break;
                                }
                        }
                        break;
                    }
                case 17:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = expressions[0].Value; }
                                    break;
                                }
                        }
                        break;
                    }
                case 19:
                    {
                        switch (step)
                        {
                            case 1:
                                {
                                    { expressions[expressions.Length - 1].Value = new BinaryExpression(BinaryExpressionType.Equal, null, expressions[1].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 20:
                    {
                        switch (step)
                        {
                            case 1:
                                {
                                    { expressions[expressions.Length - 1].Value = new BinaryExpression(BinaryExpressionType.NotEqual, null, expressions[1].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 21:
                    {
                        switch (step)
                        {
                            case 1:
                                {
                                    { if (expressions[1].Value == null) { expressions[expressions.Length - 1].Value = expressions[0].Value; } else { ((BinaryExpression)expressions[1].Value).LeftExpression = expressions[0].Value; expressions[expressions.Length - 1].Value = expressions[1].Value; } }
                                    break;
                                }
                        }
                        break;
                    }
                case 22:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = expressions[0].Value; }
                                    break;
                                }
                        }
                        break;
                    }
                case 24:
                    {
                        switch (step)
                        {
                            case 1:
                                {
                                    { expressions[expressions.Length - 1].Value = new BinaryExpression(BinaryExpressionType.Lesser, null, expressions[1].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 25:
                    {
                        switch (step)
                        {
                            case 1:
                                {
                                    { expressions[expressions.Length - 1].Value = new BinaryExpression(BinaryExpressionType.LesserOrEqual, null, expressions[1].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 26:
                    {
                        switch (step)
                        {
                            case 1:
                                {
                                    { expressions[expressions.Length - 1].Value = new BinaryExpression(BinaryExpressionType.Greater, null, expressions[1].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 27:
                    {
                        switch (step)
                        {
                            case 1:
                                {
                                    { expressions[expressions.Length - 1].Value = new BinaryExpression(BinaryExpressionType.GreaterOrEqual, null, expressions[1].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 28:
                    {
                        switch (step)
                        {
                            case 1:
                                {
                                    { if (expressions[1].Value == null) { expressions[expressions.Length - 1].Value = expressions[0].Value; } else { ((BinaryExpression)expressions[1].Value).LeftExpression = expressions[0].Value; expressions[expressions.Length - 1].Value = expressions[1].Value; } }
                                    break;
                                }
                        }
                        break;
                    }
                case 29:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = expressions[0].Value; }
                                    break;
                                }
                        }
                        break;
                    }
                case 31:
                    {
                        switch (step)
                        {
                            case 1:
                                {
                                    { expressions[expressions.Length - 1].Value = new BinaryExpression(BinaryExpressionType.Plus, null, expressions[1].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 32:
                    {
                        switch (step)
                        {
                            case 1:
                                {
                                    { expressions[expressions.Length - 1].Value = new BinaryExpression(BinaryExpressionType.Minus, null, expressions[1].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 33:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = expressions[0].Value; }
                                    break;
                                }
                            case 1:
                                {
                                    { if (expressions[1].Value != null) { expressions[expressions.Length - 1].Value = expressions[1].Value; ((BinaryExpression)expressions[expressions.Length - 1].Value).LeftExpression = expressions[0].Value; } }
                                    break;
                                }
                        }
                        break;
                    }
                case 34:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = expressions[0].Value; }
                                    break;
                                }
                        }
                        break;
                    }
                case 36:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { type = BinaryExpressionType.Times; }
                                    break;
                                }
                            case 1:
                                {
                                    { expressions[expressions.Length - 1].Value = new BinaryExpression(type, null, expressions[1].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 37:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { type = BinaryExpressionType.Div; }
                                    break;
                                }
                            case 1:
                                {
                                    { expressions[expressions.Length - 1].Value = new BinaryExpression(type, null, expressions[1].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 38:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { type = BinaryExpressionType.Modulo; }
                                    break;
                                }
                            case 1:
                                {
                                    { expressions[expressions.Length - 1].Value = new BinaryExpression(type, null, expressions[1].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 39:
                    {
                        switch (step)
                        {
                            case 1:
                                {
                                    { expressions[expressions.Length - 1].Value = new UnaryExpression(UnaryExpressionType.Not, expressions[1].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 40:
                    {
                        switch (step)
                        {
                            case 1:
                                {
                                    { expressions[expressions.Length - 1].Value = new UnaryExpression(UnaryExpressionType.Negate, expressions[1].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 41:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = expressions[0].Value; }
                                    break;
                                }
                        }
                        break;
                    }
                case 42:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = expressions[0].Value; }
                                    break;
                                }
                            case 1:
                                {
                                    { if (expressions[1].Value != null) { FindDeepest(((MemberExpression)expressions[1].Value)).Previous = expressions[0].Value; expressions[expressions.Length - 1].Value = expressions[1].Value; } }
                                    break;
                                }
                        }
                        break;
                    }
                case 43:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = expressions[0].Value; /* Test */ }
                                    break;
                                }
                        }
                        break;
                    }
                case 45:
                    {
                        switch (step)
                        {
                            case 1:
                                {
                                    { expressions[expressions.Length - 1].Value = new MemberExpression(expressions[1].Value, expressions[expressions.Length - 1].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 46:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = expressions[0].Value; }
                                    break;
                                }
                        }
                        break;
                    }
                case 47:
                    {
                        switch (step)
                        {
                            case 2:
                                {
                                    { expressions[expressions.Length - 1].Value = expressions[1].Value; }
                                    break;
                                }
                        }
                        break;
                    }
                case 48:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = expressions[0].Value; }
                                    break;
                                }
                        }
                        break;
                    }
                case 49:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = expressions[0].Value; }
                                    break;
                                }
                        }
                        break;
                    }
                case 50:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = ((Identifier)expressions[0].Value).Text == "null" ? new ValueExpression(null, TypeCode.Object) : (Expression)expressions[0].Value; }
                                    break;
                                }
                        }
                        break;
                    }
                case 51:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = expressions[0].Value; }
                                    break;
                                }
                        }
                        break;
                    }
                case 52:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = new ValueExpression(int.Parse(expressions[0].Text), TypeCode.Int32); }
                                    break;
                                }
                        }
                        break;
                    }
                case 53:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = new ValueExpression(float.Parse(expressions[0].Text, System.Globalization.NumberFormatInfo.CurrentInfo), TypeCode.Single); }
                                    break;
                                }
                        }
                        break;
                    }
                case 54:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = new ValueExpression(extractString(expressions[0].Text), TypeCode.String); }
                                    break;
                                }
                        }
                        break;
                    }
                case 55:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = new ValueExpression(expressions[0].Text, TypeCode.Boolean); }
                                    break;
                                }
                        }
                        break;
                    }
                case 56:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = expressions[0].Value; }
                                    break;
                                }
                        }
                        break;
                    }
                case 57:
                    {
                        switch (step)
                        {
                            case 1:
                                {
                                    { expressions[expressions.Length - 1].Value = new MethodCall((Identifier)expressions[0].Value, expressions[1].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 58:
                    {
                        switch (step)
                        {
                            case 4:
                                {
                                    { expressions[expressions.Length - 1].Value = new LambdaExpression((BlockExpression)expressions[1].Value, expressions[4].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 59:
                    {
                        switch (step)
                        {
                            case 2:
                                {
                                    { expressions[expressions.Length - 1].Value = new LambdaExpression(expressions[0].Value, expressions[2].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 61:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = expressions[0].Value; }
                                    break;
                                }
                        }
                        break;
                    }
                case 62:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = new Identifier(expressions[0].Text); }
                                    break;
                                }
                        }
                        break;
                    }
                case 63:
                    {
                        switch (step)
                        {
                            case 1:
                                {
                                    { ((BlockExpression)expressions[expressions.Length - 1].Value).Add(expressions[1].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 64:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = new BlockExpression(expressions[0].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 65:
                    {
                        switch (step)
                        {
                            case 4:
                                {
                                    { expressions[expressions.Length - 1].Value = new AnonymousNew(expressions[1].Text, (AnonymousParameterList)expressions[3].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 66:
                    {
                        switch (step)
                        {
                            case 2:
                                {
                                    { expressions[expressions.Length - 1].Value = new TypedNew(expressions[1].Text, ((BlockExpression)expressions[2].Value).ToArray()); }
                                    break;
                                }
                        }
                        break;
                    }
                case 67:
                    {
                        switch (step)
                        {
                            case 1:
                                {
                                    { ((AnonymousParameterList)expressions[expressions.Length - 1].Value).Add((AnonymousParameter)expressions[1].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 68:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = new AnonymousParameterList((AnonymousParameter)expressions[0].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 69:
                    {
                        switch (step)
                        {
                            case 2:
                                {
                                    { expressions[expressions.Length - 1].Value = new AnonymousParameter(expressions[0].Text, expressions[2].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 70:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = new AnonymousParameter((string)null, expressions[0].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 72:
                    {
                        switch (step)
                        {
                            case 1:
                                {
                                    { expressions[expressions.Length - 1].Value = new QueryExpression((FromClause)expressions[0].Value, (QueryBody)expressions[1].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 73:
                    {
                        switch (step)
                        {
                            case 4:
                                {
                                    { expressions[expressions.Length - 1].Value = new FromClause(expressions[1].Text, (Identifier)expressions[2].Value, expressions[4].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 74:
                    {
                        switch (step)
                        {
                            case 3:
                                {
                                    { expressions[expressions.Length - 1].Value = new FromClause(null, (Identifier)expressions[1].Value, expressions[3].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 75:
                    {
                        switch (step)
                        {
                            case 2:
                                {
                                    { expressions[expressions.Length - 1].Value = new QueryBody((ClauseList)expressions[0].Value, (SelectOrGroupClause)expressions[1].Value, (QueryContinuation)expressions[2].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 76:
                    {
                        switch (step)
                        {
                            case 1:
                                {
                                    { expressions[expressions.Length - 1].Value = new QueryBody(null, (SelectOrGroupClause)expressions[0].Value, (QueryContinuation)expressions[1].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 77:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { if (expressions[expressions.Length - 1].Value == null) { expressions[expressions.Length - 1].Value = new ClauseList(); } ((ClauseList)expressions[expressions.Length - 1].Value).Add((QueryBodyClause)expressions[0].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 78:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = expressions[0].Value; }
                                    break;
                                }
                        }
                        break;
                    }
                case 79:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = expressions[0].Value; }
                                    break;
                                }
                        }
                        break;
                    }
                case 80:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = expressions[0].Value; }
                                    break;
                                }
                        }
                        break;
                    }
                case 81:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = expressions[0].Value; }
                                    break;
                                }
                        }
                        break;
                    }
                case 82:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = expressions[0].Value; }
                                    break;
                                }
                        }
                        break;
                    }
                case 83:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = expressions[0].Value; }
                                    break;
                                }
                        }
                        break;
                    }
                case 84:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = expressions[0].Value; }
                                    break;
                                }
                        }
                        break;
                    }
                case 85:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = expressions[0].Value; }
                                    break;
                                }
                        }
                        break;
                    }
                case 87:
                    {
                        switch (step)
                        {
                            case 2:
                                {
                                    { expressions[expressions.Length - 1].Value = new QueryContinuation((Identifier)expressions[1].Value, (QueryBody)expressions[2].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 88:
                    {
                        switch (step)
                        {
                            case 1:
                                {
                                    { expressions[expressions.Length - 1].Value = new WhereClause(expressions[1].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 89:
                    {
                        switch (step)
                        {
                            case 3:
                                {
                                    { expressions[expressions.Length - 1].Value = new LetClause((Identifier)expressions[1].Value, expressions[3].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 90:
                    {
                        switch (step)
                        {
                            case 9:
                                {
                                    { expressions[expressions.Length - 1].Value = new JoinClause(expressions[1].Text, (Identifier)expressions[2].Value, expressions[4].Value, expressions[6].Value, expressions[8].Value, (Identifier)expressions[9].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 91:
                    {
                        switch (step)
                        {
                            case 8:
                                {
                                    { expressions[expressions.Length - 1].Value = new JoinClause(null, (Identifier)expressions[1].Value, expressions[3].Value, expressions[5].Value, expressions[7].Value, (Identifier)expressions[8].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 92:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = expressions[0].Value; }
                                    break;
                                }
                        }
                        break;
                    }
                case 94:
                    {
                        switch (step)
                        {
                            case 1:
                                {
                                    { expressions[expressions.Length - 1].Value = expressions[1].Value; }
                                    break;
                                }
                        }
                        break;
                    }
                case 95:
                    {
                        switch (step)
                        {
                            case 2:
                                {
                                    { expressions[expressions.Length - 1].Value = new OrderByCriteria(expressions[1].Value, expressions[2].Text == null || expressions[2].Text == "ascending"); }
                                    break;
                                }
                        }
                        break;
                    }
                case 96:
                    {
                        switch (step)
                        {
                            case 1:
                                {
                                    { ((OrderByClause)expressions[expressions.Length - 1].Value).Add((OrderByCriteria)expressions[2].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 97:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = new OrderByClause((OrderByCriteria)expressions[0].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 98:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = expressions[0].Value; }
                                    break;
                                }
                        }
                        break;
                    }
                case 100:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = expressions[0].Value; }
                                    break;
                                }
                        }
                        break;
                    }
                case 101:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = expressions[0].Value; }
                                    break;
                                }
                        }
                        break;
                    }
                case 102:
                    {
                        switch (step)
                        {
                            case 1:
                                {
                                    { expressions[expressions.Length - 1].Value = new SelectClause(expressions[1].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 103:
                    {
                        switch (step)
                        {
                            case 3:
                                {
                                    { expressions[expressions.Length - 1].Value = new GroupClause((Identifier)expressions[1].Value, expressions[3].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 104:
                    {
                        switch (step)
                        {
                            case -1:
                                {
                                    { EnableTextMode(); }
                                    break;
                                }
                            case 0:
                                {
                                    { DisableTextMode(); }
                                    break;
                                }
                        }
                        break;
                    }
                case 105:
                    {
                        switch (step)
                        {
                            case -1:
                                {
                                    { EnableTextMode(); }
                                    break;
                                }
                            case 1:
                                {
                                    { DisableTextMode(); }
                                    break;
                                }
                        }
                        break;
                    }
                case 106:
                    {
                        switch (step)
                        {
                            case -1:
                                {
                                    { EnableTextMode(); }
                                    break;
                                }
                            case 0:
                                {
                                    { DisableTextMode(); }
                                    break;
                                }
                        }
                        break;
                    }
                case 108:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { if (!(expressions[expressions.Length - 1].Value is BlockExpression)) { expressions[expressions.Length - 1].Value = new BlockExpression(expressions[expressions.Length - 1].Value); } }
                                    break;
                                }
                            case 1:
                                {
                                    { ((BlockExpression)expressions[expressions.Length - 1].Value).Add(expressions[1].Value); }
                                    break;
                                }
                        }
                        break;
                    }
                case 109:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = expressions[0].Value; }
                                    break;
                                }
                        }
                        break;
                    }
                case 110:
                    {
                        switch (step)
                        {
                            case 2:
                                {
                                    { expressions[expressions.Length - 1].Value = expressions[1].Value; }
                                    break;
                                }
                        }
                        break;
                    }
                case 111:
                    {
                        switch (step)
                        {
                            case 0:
                                {
                                    { expressions[expressions.Length - 1].Value = expressions[0].Value; }
                                    break;
                                }
                        }
                        break;
                    }
            }
        }


        private BinaryExpressionType type;
        private const char BS = '\\';

        private string extractString(string text)
        {

            StringBuilder sb = new StringBuilder(text);
            int startIndex = 1; // Skip initial quote
            int slashIndex = -1;

            while ((slashIndex = sb.ToString().IndexOf(BS, startIndex)) != -1)
            {
                char escapeType = sb[slashIndex + 1];
                switch (escapeType)
                {
                    case 'u':
                        string hcode = String.Concat(sb[slashIndex + 4], sb[slashIndex + 5]);
                        string lcode = String.Concat(sb[slashIndex + 2], sb[slashIndex + 3]);
                        char[] unicodeChar = Encoding.Unicode.GetChars(new byte[] { System.Convert.ToByte(hcode, 16), System.Convert.ToByte(lcode, 16) });
                        // Should be an array of 1 item
                        sb.Remove(slashIndex, 6).Insert(slashIndex, unicodeChar);
                        break;
                    case 'n': sb.Remove(slashIndex, 2).Insert(slashIndex, new char[] { '\n' }); break;
                    case 'r': sb.Remove(slashIndex, 2).Insert(slashIndex, new char[] { '\r' }); break;
                    case 't': sb.Remove(slashIndex, 2).Insert(slashIndex, new char[] { '\t' }); break;
                    case '\'': sb.Remove(slashIndex, 2).Insert(slashIndex, new char[] { '\'' }); break;
                    case '\\': sb.Remove(slashIndex, 2).Insert(slashIndex, new char[] { '\\' }); break;
                    default: throw new InvalidSyntaxException("Unvalid escape sequence: " + escapeType);
                }

                startIndex = slashIndex + 1;

            }

            sb.Remove(0, 1);
            sb.Remove(sb.Length - 1, 1);

            return sb.ToString();
        }


    }
}
